package no.ffi.gismoidm.invocations;

import java.io.Serializable;
import java.util.UUID;
import no.ffi.gismoidm.utils.IdentityStatement;
import no.ffi.gismoidm.utils.GismoIdmException;
import no.ffi.tpm.crypto.CryptoOperations;

public class ServiceInvocationFactory {

    public static final int STATELESS=1;
    public static final int STATEFUL=2;
    
    private int protocolClass;

    public ServiceInvocationFactory(int protocolClass) throws GismoIdmException {
        if (protocolClass != STATELESS &&
                protocolClass != STATEFUL)
            throw new GismoIdmException("Illegal constroctur parameter");
        this.protocolClass = protocolClass; 
    }

    public ServiceInvocationFactory(String protocolClass) throws GismoIdmException {
        if (protocolClass.equalsIgnoreCase("STATEFUL"))
            this.protocolClass = STATEFUL;
        else if (protocolClass.equalsIgnoreCase("STATELESS"))
            this.protocolClass = STATELESS;
        else
            throw new GismoIdmException("Illegal constroctur parameter");
    }
    
    public ServiceInvocation createRequestInstance(Serializable parameters, IdentityStatement localAss, CryptoOperations crOp, long clockcounter) throws GismoIdmException {
        if (protocolClass==STATELESS)
            return new ServiceInvocationStateless(parameters, localAss, crOp);
        else if (protocolClass==STATEFUL)
            return new ServiceInvocationStateful(parameters, localAss, crOp,clockcounter);
        else
            throw new GismoIdmException("Illegal protocol class for invocation factory");

    }
    public ServiceInvocation createResponseInstance(Serializable returns, IdentityStatement clientAss, IdentityStatement serverAss, UUID nonce, CryptoOperations crOp) throws Exception {
        if (protocolClass==STATELESS)
            return new ServiceInvocationStateless(returns, clientAss, serverAss, nonce, crOp);
        else if (protocolClass==STATEFUL)
            return new ServiceInvocationStateful(returns, serverAss, nonce, crOp);
        else
            throw new GismoIdmException("Illegal protocol class for invocation factory");
    }

    public ServiceInvocation createCrossResponseInstance(Serializable returns, IdentityStatement clientAss, IdentityStatement serverAss, IdentityStatement crossAss, UUID nonce, CryptoOperations crOp) throws Exception {
        if (protocolClass==STATELESS)
            return new ServiceInvocationStateless(returns, clientAss, serverAss, crossAss, nonce, crOp);
        else if (protocolClass==STATEFUL)
            return new ServiceInvocationStateful(returns, serverAss, crossAss, nonce, crOp);
        else
            throw new GismoIdmException("Illegal protocol class for invocation factory");
    }
}
